package org.smile.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.log4j.Logger;
/**
 * 可以缓冲的字符流
 * @author strive
 *
 */
public class BufferedReader{
	
    protected static final Logger logger=Logger.getLogger(BufferedReader.class);
    
	private InputStream is;
	private InputStreamReader isr;
	private java.io.BufferedReader br;
	
	public BufferedReader(Reader reader) {
		this.br=new java.io.BufferedReader(reader);
	}
	/**
	 * 从一个文件创建一个流
	 * @param file
	 * @throws FileNotFoundException 
	 */
	public BufferedReader(File file) throws FileNotFoundException{
		try {
			this.is=new FileInputStream(file);
			this.isr=new InputStreamReader(is);
			this.br=new java.io.BufferedReader(isr);
		} catch (FileNotFoundException e) {
			throw new FileNotFoundException("从文件"+file.getName()+"实现化流出错"+e);
		}
	}
	/**
	 * 从一个文件创建一个流
	 * @param file
	 * @throws FileNotFoundException 
	 */
	public BufferedReader(String file) throws FileNotFoundException{
		try {
			this.is=new FileInputStream(file);
			this.isr=new InputStreamReader(is);
			this.br=new java.io.BufferedReader(isr);
		} catch (FileNotFoundException e) {
			throw new FileNotFoundException("从文件"+file+"实现化流出错"+e);
		}
	}
	/**
	 * 从一个输入流创建
	 * @param is
	 */
	public BufferedReader(InputStream is){
		this.is=is;
		this.isr=new InputStreamReader(is);
		this.br=new java.io.BufferedReader(isr);
	}
	/**
	 * 读取一行
	 * @return
	 * @throws IOException
	 */
	public String readLine() throws IOException{
		return this.br.readLine();
	}
	/**
	 * 读取数据到一个字符数组中
	 * @param b
	 * @return
	 * @throws IOException
	 */
	public int read(char[] b) throws IOException{
		return this.br.read(b);
	}
	/**
	 * 关闭连接
	 */
	public void close(){
		try{
			if(this.br!=null){
				this.br.close();
			}
			if(isr!=null){
				this.isr.close();
			}
			if(is!=null){
				this.is.close();
			}
		}catch(IOException e){
			logger.error("关闭流了出错",e);
		}
	}
	
	public java.io.BufferedReader getBr() {
		return br;
	}
}
